//
//  YWNewfeatureViewController.m
//  maintain
//
//  Created by 郑想 on 15/10/19.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "YWNewfeatureViewController.h"
#import "YWTabBarViewController.h"
#import "UIView+Extension.h"
#import "YWMacro.h"

#define HWNewfeatureCount 4

@interface YWNewfeatureViewController () <UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl* pageControl;

@property (nonatomic, weak) UIScrollView* scrollView;
@end

@implementation YWNewfeatureViewController
#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIScrollView* scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;

    CGFloat scrollW = scrollView.width;
    CGFloat scrollH = scrollView.height;
    for (int i = 0; i < HWNewfeatureCount; i++) {
        UIImageView* imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height = scrollH;
        imageView.y = 0;
        imageView.x = i * scrollW;

        NSString* name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];

        if (i == HWNewfeatureCount - 1) {
            [self setupLastImageView:imageView];
        }
    }

    scrollView.contentSize = CGSizeMake(HWNewfeatureCount * scrollW, 0);
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;

    //    UIPageControl *pageControl = [[UIPageControl alloc] init];
    //    pageControl.numberOfPages = HWNewfeatureCount;
    //    pageControl.backgroundColor = [UIColor redColor];
    //    pageControl.currentPageIndicatorTintColor = YWColor(253, 98, 42);
    //    pageControl.pageIndicatorTintColor = YWColor(189, 189, 189);
    //    pageControl.centerX = scrollW * 0.5;
    //    pageControl.centerY = scrollH - 50;
    //    [self.view addSubview:pageControl];
    //    self.pageControl = pageControl;
}
#pragma mark -UIScrollViewDelegate methods
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
    double page = scrollView.contentOffset.x / scrollView.width;
    // 四舍五入计算出页码
    self.pageControl.currentPage = (int)(page + 0.5);
}
#pragma mark -common methods
/**
 *  初始化最后一个imageView
 *
 *  @param imageView 最后一个imageView
 */
- (void)setupLastImageView:(UIImageView*)imageView
{
    // 开启交互功能
    imageView.userInteractionEnabled = YES;

    // 开始体验
    UIButton* startBtn = [[UIButton alloc] init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    startBtn.size = startBtn.currentBackgroundImage.size;
    startBtn.centerX = SCREEN_WIDTH / 2;
    startBtn.centerY = imageView.height - 60;
    [startBtn setTitle:@"开始体验" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}

- (void)startClick
{
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[YWTabBarViewController alloc] init];
}
@end
